#!/bin/bash
#
# Copyright (C) 2017 ~ 2018 Lingmo Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Add user to group which is found in /etc/group
add_user_to_existed_group() {
  local USER=$1
  local GROUP=$2
  if grep -q "^${GROUP}:" /etc/group 2>/dev/null; then
    gpasswd -a "${USER}" "${GROUP}"
  else
    warn "No group found ${GROUP}"
  fi
}

# Setup username, password and hostname.
setup_username_password_hostname() {
  local DI_HOSTNAME DI_PASSWORD DI_USERNAME REAL_PASSWORD PASSWORD_MIN_LEN
  DI_HOSTNAME=$(installer_get "DI_HOSTNAME")
  DI_PASSWORD=$(installer_get "DI_PASSWORD")
  DI_USERNAME=$(installer_get "DI_USERNAME")
  PASSWORD_MIN_LEN=$(installer_get "system_info_password_min_len")

  [ -z "${DI_USERNAME}" ] && error "Variable DI_USERNAME not set. Abort!"
   if [ x"${PASSWORD_MIN_LEN}" != x0 -a -z "${DI_PASSWORD}" ]; then
    error "Variable DI_PASSWORD not set. Abort!"
  fi
  DI_HOSTNAME="${DI_HOSTNAME:-lingmo}"

  # Reset password in settings file
  installer_set "system_info_default_password" ""
  installer_set "DI_PASSWORD" ""

  useradd -U -m --skel /etc/skel --shell /bin/bash ${DI_USERNAME}

  # Update password.
  if [ -z "${DI_PASSWORD}" ]; then
      msg "set password to empty"
      passwd -d "${DI_USERNAME}"
  else
    REAL_PASSWORD=$(echo "${DI_PASSWORD}" | base64 -d)
    echo "${DI_USERNAME}:${REAL_PASSWORD}" | chpasswd
  fi

  # Add user to groups.
  set +e
  add_user_to_existed_group "${DI_USERNAME}" lp
  add_user_to_existed_group "${DI_USERNAME}" lpadmin
  add_user_to_existed_group "${DI_USERNAME}" netdev
  add_user_to_existed_group "${DI_USERNAME}" network
  add_user_to_existed_group "${DI_USERNAME}" sambashare
  add_user_to_existed_group "${DI_USERNAME}" scanner
  add_user_to_existed_group "${DI_USERNAME}" storage
  add_user_to_existed_group "${DI_USERNAME}" sudo
  add_user_to_existed_group "${DI_USERNAME}" users
  add_user_to_existed_group "${DI_USERNAME}" wheel
  set -e

  # Chown owner and group to current user if uid mismatch.
  USER_HOME=/home/${DI_USERNAME}
  if [[ -d "${USER_HOME}" ]]; then
    USER_ID=$(id -u "${DI_USERNAME}")
    GROUP_ID=$(id -g "${DI_USERNAME}")
    FOLDER_UID=$(stat -c "%u" "${USER_HOME}")
    FOLDER_GID=$(stat -c "%g" "${USER_HOME}")
    msg "${USER_HOME}: ${USER_ID}:${GROUP_ID}:${FOLDER_UID}:${FOLDER_GID}"

    if [ "${USER_ID}" -ne "${FOLDER_UID}" -o \
      "${GROUP_ID}" -ne "${FOLDER_GID}" ]; then
      msg "Fix HOME directory permission at ${DI_USERNAME}."
      chown -hR "${DI_USERNAME}:${DI_USERNAME}" "${USER_HOME}"
    fi
  else
    warn "${USER_HOME} does not exist"
  fi

  # Setup hostname.
  msg "Set Hostname to ${DI_HOSTNAME}"
  echo "${DI_HOSTNAME}" | tee /etc/hostname

  cat > /etc/hosts << EOF
127.0.0.1	localhost
127.0.1.1   ${DI_HOSTNAME}

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
}

# Setup in in_chroot, used in first boot setup phrase, 
# because X & X Clients crash without hostname set.
# 
# I'll just leave it without cleaning since it'll be override 
# in real user setup(see code above).
setup_first_boot_hostname() {
  msg "Setup first boot temporary hostname"
  echo "first-boot-hostname" | tee /etc/hostname
  cat > /etc/hosts << EOF
127.0.0.1	localhost
127.0.1.1 first-boot-hostname
EOF
}

DI_STRONG_PASSWORD=$(installer_get "DI_STRONG_PASSWORD")
cat > /etc/lingmo/dde-control-center.conf << EOF
[Password]
STRONG_PASSWORD=${DI_STRONG_PASSWORD}
EOF

if [ x$(installer_get "system_info_setup_after_reboot") != "xtrue" ]; then
  setup_username_password_hostname
else
  setup_first_boot_hostname
fi
